﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.BikeManage;
using DataAccess.Common;
using Ninject;
using WinForm.Helper;

namespace WinForm.Dialog
{
    public partial class EditLockPile : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();
        private readonly ILockPile _lockPile = Kernel.Get<ILockPile>();
        private readonly ILookUpDataService _lookUpDataService = Kernel.Get<LookUpDataService>();


        public EditLockPile()
        {
            InitializeComponent();
        }

        public EditLockPile(int id)
        {
            InitializeComponent();

            if (id > 0)
            {
                lbPileId.Text = id.ToString();
                ResultInfo<jsh_lockpile> pile = _lockPile.GetLockPile(id);
                txtPileNo.Text = pile.Obj.Pileno;
                txtIpAddress.Text = pile.Obj.Ip;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtPileNo.Text) || string.IsNullOrWhiteSpace(txtPileNo.Text))
            {
                MyMessageBox.ShowMessageBoxInfo("请输入锁柱编号！");
                return;
            }


            try
            {
                var lockpile = new jsh_lockpile
                {
                    id = Converts.ConvertToInt(lbPileId.Text),
                    Ip = txtIpAddress.Text,
                    Pileno = txtPileNo.Text,
                    Status = cmbStatus.SelectedValue == null ? "" : cmbStatus.SelectedValue.ToString(),
                    Controlbox = cmbControlBox.SelectedValue == null ? "" : cmbControlBox.SelectedValue.ToString(),
                    Area = cmbControlBox.Text,
                    Areaid = Converts.ConvertToInt(cmbControlBox.SelectedValue == null
                            ? ""
                            : cmbControlBox.SelectedValue.ToString())
                };
                ResultInfo<int> id = _lockPile.CreateOrUpdateLockPile(lockpile);
                if (id.ResultNumber == ResultEnum.Success)
                {
                    if (!string.IsNullOrEmpty(lbPileId.Text))
                    {
                        MyMessageBox.ShowMessageBoxInfo("修改锁柱信息成功！");
                    }
                    if (string.IsNullOrEmpty(lbPileId.Text))
                    {
                        MyMessageBox.ShowMessageBoxInfo("新增锁柱信息成功！");
                    }
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MyMessageBox.ShowMessageBoxInfo(id.Describe);
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxInfo("保存失败！");
            }
        }

        private void EditLockPile_Load(object sender, EventArgs e)
        {
            try
            {
                ResultInfo<IList<jsh_Area>> areas = _lockArea.GetLockArea(0);
                if (areas.ResultNumber == ResultEnum.Success)
                {
                    cmbControlBox.DataSource = areas.Obj;
                    cmbControlBox.DisplayMember = "Area";
                    cmbControlBox.ValueMember = "id";

                    cmbLockAddress.DataSource = areas.Obj;
                    cmbLockAddress.DisplayMember = "Area";
                    cmbLockAddress.ValueMember = "id";
                }

                List<IdNameObject> status = _lookUpDataService.GetLockaddressStatus();
                cmbStatus.DataSource = status;
                cmbStatus.DisplayMember = "Name";
                cmbStatus.ValueMember = "Id";

                cmbVersion.SelectedIndex = 0;
            }
            catch (Exception)
            {
            }
        }

        private void btnPileCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }
    }
}